Skip to content

fix(viewer-context): Populate org after request resolution#113139

Open
gricha wants to merge 3 commits intomasterfrom
gricha/fix/viewer-context-org-resolution
Open

fix(viewer-context): Populate org after request resolution#113139
gricha wants to merge 3 commits intomasterfrom
gricha/fix/viewer-context-org-resolution

Conversation

@gricha
Copy link
Copy Markdown
Member

@gricha gricha commented Apr 15, 2026

Populate ViewerContext.organization_id when the request organization is
resolved in endpoint convert_args paths.

ViewerContextMiddleware runs before URL-scoped organization resolution, so
session-authenticated requests could carry a user in the contextvar without an
organization even once the request org had been bound later in dispatch. This
updates the context at the same points where we already set
request._request.organization for organization, project, team, group, and
monitor endpoints.

I considered trying to resolve the organization earlier in middleware, but that
would duplicate endpoint-specific lookup logic and still miss cases where the
organization only becomes known through project or group resolution. Keeping the
enrichment at the existing binding sites is narrower and matches the canonical
request lifecycle.

This also adds focused regressions for the shared helper and for the
organization, project, team, and group endpoint resolution paths so the context
keeps the resolved org once convert_args has run.

Set the viewer context organization id at the same points where API request
handlers resolve and bind the request organization.

This keeps the early middleware behavior intact while making the resolved
organization available to code that runs after convert_args on organization,
project, team, group, and monitor endpoints. Add focused regression tests for
the shared helper and the main endpoint resolution paths.

Co-Authored-By: Codex <codex@openai.com>
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 15, 2026
Update the viewer context organization id inside bind_organization_context
instead of repeating the same call at each binding site.

This keeps request-scoped enrichment behavior the same while making the
canonical organization binding hook responsible for both SDK scope tags and
ambient viewer context updates.

Co-Authored-By: Codex <codex@openai.com>
@gricha gricha marked this pull request as ready for review April 16, 2026 00:44
@gricha gricha requested review from a team as code owners April 16, 2026 00:44
Keep the endpoint viewer-context regression tests type-safe by separating the
raw WSGI request from the DRF request wrapper used for convert_args.

This matches the actual request shapes under test and fixes the backend typing
job without changing runtime behavior.

Co-Authored-By: Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant